/*
 * Copyright (c) 2017 Solidwang. All Rights Reserved
 */
package com.solid4j.auth.controller;

import com.solid4j.auth.model.Menu;
import com.solid4j.auth.service.MenuService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * @author: solidwang
 * @date: 17/5/4
 */
@Controller
@RequestMapping("/auth/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1", required = true) Integer currentPage,
                       Model model) {
        Map<Menu, List<Menu>> menuMap = new LinkedHashMap<>();
        List<Menu> firstMenuList = menuService.getMenuListByParentId(0);
        if(firstMenuList != null) {
            for (Menu menu : firstMenuList) {
                List<Menu> secondMenuList = menuService.getMenuListByParentId(menu.getId());
                menuMap.put(menu, secondMenuList);
            }
        }
        model.addAttribute("menuMap", menuMap);
        return "/menu/list";
    }

    @RequestMapping("/redirect")
    public String redirect(@RequestParam(defaultValue = "save", required = true) String type, @RequestParam(value = "id", required = false) Integer id, Model model) {
        //一级菜单列表
        List<Menu> menuList = menuService.getMenuListByParentId(0);
        model.addAttribute("menuList", menuList);
        if ("save".equals(type)) {
            return "/menu/save";
        } else if ("update".equals(type)) {
            Menu menu= menuService.get(id);
            model.addAttribute("menu", menu);
            return "/menu/update";
        }
        return null;
    }

    @RequestMapping("/save")
    public String save(@ModelAttribute Menu menu) {
        menuService.save(menu);
        return "redirect:/auth/menu/list.do?currentPage=1";
    }

    @RequestMapping("/update")
    public String update(@ModelAttribute Menu menu, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage) {
        menuService.update(menu);
        return "redirect:/auth/menu/list.do?currentPage=" + currentPage;
    }


    @RequestMapping("/delete")
    public String delete(@RequestParam(value = "id") Integer id,
                         @RequestParam(value = "currentPage", defaultValue = "1") int currentPage) {
        Menu menu = menuService.get(id);
        menuService.delete(menu);
        //删除父菜单及其子菜单
        List<Menu> menuList = menuService.getMenuListByParentId(id);
        if(menuList != null && menuList.size()>0) {
            for(Menu child : menuList) {
                menuService.delete(child);
            }
        }
        return "redirect:/auth/menu/list.do?currentPage=" + currentPage;
    }
}
